package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.nfcs.NfcsData;
import com.ucredit.babel.common.model.nfcs.NfcsReport;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @Classname ShangHaiNfcsReportTransaction
 * @Description 上海资信报告-贷款交易信息
 * @Date 2021/5/27 21:11
 * @Created by lzw
 */
public class ShangHaiNfcsReportTransaction extends ParseBase {

    Text key = new Text();
    Text value = new Text();

    @Override
    public boolean parseJob(Mapper.Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        NfcsReport obj = (NfcsReport) model;
        NfcsData data = obj.getData();

        StringBuilder sb = getBaseInfo(obj, successful);
        StringBuilder TxSb = new StringBuilder(500);

        JSONObject info = data.getData() == null ? new JSONObject() : JSON.parseObject(data.getData());
        JSONObject loanTransactionInformation = info.getString("loanTransactionInformation") == null ? new JSONObject() : JSON.parseObject(info.getString("loanTransactionInformation"));

        JSONArray loan = loanTransactionInformation.getJSONArray("loanRecords") == null ? new JSONArray() : loanTransactionInformation.getJSONArray("loanRecords");
        if (!loan.isEmpty()) {
            for (int i = 0; i < loan.size(); i++) {
                JSONObject loanInfo = loan.getString(i) == null ? new JSONObject() : JSON.parseObject(loan.getString(i));
                TxSb.delete(0, TxSb.length());
                TxSb.append(loanInfo.getString("loanProject") == null ? "" : loanInfo.getString("loanProject")).append(CRT001);
                TxSb.append(loanInfo.getString("institutionName") == null ? "" : loanInfo.getString("institutionName")).append(CRT001);
                TxSb.append(loanInfo.getString("creditLine") == null ? "" : loanInfo.getString("creditLine")).append(CRT001);
                TxSb.append(loanInfo.getString("guaranteeMode") == null ? "" : loanInfo.getString("guaranteeMode")).append(CRT001);
                TxSb.append(loanInfo.getString("openDate") == null ? "" : loanInfo.getString("openDate")).append(CRT001);
                TxSb.append(loanInfo.getString("currency") == null ? "" : loanInfo.getString("currency")).append(CRT001);
                TxSb.append(loanInfo.getString("occurred") == null ? "" : loanInfo.getString("occurred")).append(CRT001);
                TxSb.append(loanInfo.getString("shareCreditLine") == null ? "" : loanInfo.getString("shareCreditLine")).append(CRT001);
                TxSb.append(loanInfo.getString("maximumDebt") == null ? "" : loanInfo.getString("maximumDebt")).append(CRT001);
                TxSb.append(loanInfo.getString("repaymentFrequency") == null ? "" : loanInfo.getString("repaymentFrequency")).append(CRT001);
                TxSb.append(loanInfo.getString("endingBalance") == null ? "" : loanInfo.getString("endingBalance")).append(CRT001);
                TxSb.append(loanInfo.getString("remainingRepaymentMonths") == null ? "" : loanInfo.getString("remainingRepaymentMonths")).append(CRT001);
                TxSb.append(loanInfo.getString("currentPaymentDate") == null ? "" : loanInfo.getString("currentPaymentDate")).append(CRT001);
                TxSb.append(loanInfo.getString("currentPaymentAmount") == null ? "" : loanInfo.getString("currentPaymentAmount")).append(CRT001);
                TxSb.append(loanInfo.getString("accountStatus") == null ? "" : loanInfo.getString("accountStatus")).append(CRT001);
                TxSb.append(loanInfo.getString("actualRepaymentDate") == null ? "" : loanInfo.getString("actualRepaymentDate")).append(CRT001);
                TxSb.append(loanInfo.getString("actualRepaymentAmount") == null ? "" : loanInfo.getString("actualRepaymentAmount")).append(CRT001);
                TxSb.append(loanInfo.getString("currentOverdueAmount") == null ? "" : loanInfo.getString("currentOverdueAmount")).append(CRT001);
                TxSb.append(loanInfo.getString("currentOverduePeriod") == null ? "" : loanInfo.getString("currentOverduePeriod")).append(CRT001);
                TxSb.append(loanInfo.getString("accumulateOverduePeriod") == null ? "" : loanInfo.getString("accumulateOverduePeriod")).append(CRT001);
                TxSb.append(loanInfo.getString("maximumOverduePeriod") == null ? "" : loanInfo.getString("maximumOverduePeriod")).append(CRT001);
                TxSb.append(loanInfo.getString("paymentStatusWithin24Months") == null ? "" : loanInfo.getString("paymentStatusWithin24Months")).append(CRT001);
                TxSb.append(loanInfo.getString("overdueAmountBetween31And60days") == null ? "" : loanInfo.getString("overdueAmountBetween31And60days")).append(CRT001);
                TxSb.append(loanInfo.getString("overdueAmountBetween61And90days") == null ? "" : loanInfo.getString("overdueAmountBetween61And90days")).append(CRT001);
                TxSb.append(loanInfo.getString("overdueAmountBetween91And180days") == null ? "" : loanInfo.getString("overdueAmountBetween91And180days")).append(CRT001);
                TxSb.append(loanInfo.getString("overdueAmountGt180days") == null ? "" : loanInfo.getString("overdueAmountGt180days")).append(CRT001);
                TxSb.append(loanInfo.getString("getDate") == null ? "" : loanInfo.getString("getDate")).append(CRT001);
                key.set(getKey());
                value.set((sb.toString() + TxSb.toString()).replace("\n", " ").replace("\r", " "));
                context.write(key, value);
            }
        }
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_nfcs_report_transaction_inc";
    }

    @Override
    public String getType() {
        return RecordType.NFCS_REPORT.toString();
    }

    @Override
    public String getSubType() {
        return ".transaction";
    }

    @Override
    public String getComment() {
        return "上海资信报告-贷款交易信息";
    }
}
